package com.rock.sys.controller;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.StringTemplateResourceLoader;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Response;
import com.eova.common.render.Html2XlsRender;
import com.eova.common.utils.xx;
import com.eova.config.EovaConst;
import com.eova.model.MetaObject;
import com.eova.service.sm;
import com.eova.template.common.util.TemplateUtil;
import com.eova.template.office.OfficeIntercept;
import com.eova.widget.WidgetManager;
import com.eova.widget.WidgetUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.rock.sys.service.RockMetaServUtil;
import com.rock.sys.util.RockUtil;

public class RockOfficeControllerbak extends Controller {
	final Controller ctrl = this;
	protected OfficeIntercept intercept = null;
	
	/**
	 * 前往文档查看页面
	 */
	public void goDoc() {
		AopContext ac = new AopContext(this);

		String objectCode = this.getPara(0);
		// 获取主键的值
		Object pkValue = getPara(1);
		
		MetaObject object = sm.meta.getMeta(objectCode);

		// 根据主键获取对象
		Record record = null;
		if(!xx.isEmpty(pkValue))
		 record = Db.use(object.getDs()).findById(object.getView(), object.getPk(), pkValue);
		
		if(record == null){
			record = new Record();
		}
			
		setAttr("record", record);
		setAttr("object", object);
		
		render("/rock/template/single/doc.html");
	}

	/**
	 * 显示office页面（html+beetl渲染）
	 * @throws Exception
	 */
	public void showDoc() throws Exception {
		AopContext ac = new AopContext(this);

		String objectCode = this.getPara(0);
		// 获取主键的值
		Object pkValue = getPara(1);
		
		MetaObject object = sm.meta.getMeta(objectCode);

		// 根据主键获取对象
		Record record = null;
		if(!xx.isEmpty(pkValue))
		 record = Db.use(object.getDs()).findById(object.getView(), object.getPk(), pkValue);
		
		if(record == null){
			record = new Record();
		}
			
		ac.object = object;
		ac.record = record;
		
		MetaObjectIntercept intercept = TemplateUtil.initMetaObjectIntercept(object.getBizIntercept());
		if (intercept != null) {
			String msg = intercept.initDoc(ac);
			if (!xx.isEmpty(msg)) {
				renderJson(new Response(Response.STATUS_FAIL,"文档查看前置拦截执行异常!" + msg));
				return;
			}
		}

		// 根据表达式将数据中的值翻译成汉字
		RockUtil.convertRecordValue(ac.record, object, this);

		Record orc = Db.use(EovaConst.DS_EOVA).findFirst("select * from eova_object where code=?", object.getCode());
		
		//使用beetl模板语言生成doc文档，从文本创建模板
        //此处无法使用jfinal enjoy模板，word转换后的html中有enjoy无法识别的#字符，转换会报错
		StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
		Configuration cfg = Configuration.defaultConfiguration();
		GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
		//获取模板
		org.beetl.core.Template t = gt.getTemplate(orc.getStr("dochtml"));
		t.binding("data", ac.record);
		//渲染成html
		String resHtml  = t.render();
		
		//返回给前端
		this.renderHtml(resHtml);
		
	}

	/**
	 * 下载office 文件
	 * @throws Exception
	 */
	public void downloadDoc() throws Exception {

		AopContext ac = new AopContext(this);

		String objectCode = this.getPara(0);
		// 获取主键的值
		Object pkValue = getPara(1);
		
		MetaObject object = sm.meta.getMeta(objectCode);
		
		File metaDocFile = RockMetaServUtil.getMetaDocFile(object, pkValue, ac);

		this.renderFile(metaDocFile, object.getName()+"-"+pkValue + ".docx");
		
	}
	
	
}
